package cc.mrbird.plan.controller;


import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.plan.domain.CheckListType;
import cc.mrbird.plan.service.CheckTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Api(description = "自查类型管理接口")
@Controller
public class CheckTypeController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CheckTypeService checkTypeService;
    @Log("获取自查类型信息")
    @RequestMapping(value = "checktype",method = RequestMethod.POST)
    /* @RequiresPermissions("checktype:list")*/
    public String index() {
        return "assets/checktype/checktype";
    }
    @Log("获取自查类型信息")
    @ApiOperation(value = "获取自查类型信息" ,  notes="获取自查类型信息")
    @RequestMapping(value = "checktype/list",method = RequestMethod.POST)
    /*@RequiresPermissions("checktype:list")*/
    @ResponseBody
    public Map<String, Object> checktypeList(QueryRequest request, CheckListType checkListType) {
        return super.selectByPageNumSize(request, () -> this.checkTypeService.findAllList(checkListType, request));
    }
    @ApiOperation(value = "自查类型添加")
    @RequestMapping(value = "checktype/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(CheckListType checkListType) {
        try {
            CheckListType result = this.checkTypeService.findByName(checkListType.getCheck_name());
            if (result != null) {
                return ResponseBo.warn("该自查类型已存在！");
            }
            this.checkTypeService.add(checkListType);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "修改自查类型信息")
    @Log("修改自查类型信息")
    /* @RequiresPermissions("checktype:update")*/
    @RequestMapping(value = "checktype/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(CheckListType checkListType) {
        try {
            this.checkTypeService.update(checkListType);
            return ResponseBo.ok("修改自查类型信息成功！");
        } catch (Exception e) {
            log.error("修改自查类型信息失败", e);
            return ResponseBo.error("修改自查类型信息失败，请联系网站管理员！");
        }
    }
    @Log("删除自查类型信息")
    /*@RequiresPermissions("checktype:delete")*/
    @RequestMapping(value = "checktype/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.checkTypeService.delete(ids);
            return ResponseBo.ok("删除自查类型信息成功！");
        } catch (Exception e) {
            log.error("删除自查类型信息失败", e);
            return ResponseBo.error("删除自查类型 信息失败，请联系网站管理员！");
        }
    }
}
